<?php
header ( "Content-type: text/html; charset= UTF-8" );
require_once '../../includes/global.php';
//error_reporting(E_ALL);
$real_time_table = 'se_price';
$time_field = 'date';
$num_field = 'price';

$bdid = $_GET ['bdid'];
$bdb = $_GET ['tid'];
switch ($_GET ['op']) {
    case 'next' :
        $temp_ts = $_GET ['ts'];
        $temp_month = date ( "n", $temp_ts ) + 1;
        $temp_year = date ( 'Y', $temp_ts );
        $timestamp = mktime ( 0, 0, 0, $temp_month, 1, $temp_year );
        break;
    case 'last' :
        $temp_ts = $_GET ['ts'];
        $temp_month = date ( "n", $temp_ts ) - 1;
        $temp_year = date ( 'Y', $temp_ts );
        $timestamp = mktime ( 0, 0, 0, $temp_month, 1, $temp_year );
        break;
    case 'forward' :
        $timestamp = mktime ( 0, 0, 0, $_GET ['month'], 1, $_GET ['year'] );
        break;
    
    case 'week' :
        $arr = explode ( '-', $_GET ['startWeekDate'] );
        $s_year = $arr [0];
        $s_month = $arr [1];
        $s_day = $arr [2];
        $arr = explode ( '-', $_GET ['endWeekDate'] );
        $e_year = $arr [0];
        $e_month = $arr [1];
        $e_day = $arr [2];
        $s_ts = mktime ( 0, 0, 0, $s_month, $s_day, $s_year );
        $e_ts = mktime ( 0, 0, 0, $e_month, $e_day, $e_year );
        
        $var_week_num = $_GET ['week_num'];
        // echo count($var_week_num);
        for($i = 0; $i < count ( $var_week_num ); $i ++) {
            if (isset ( $var_week_num [$i] ) && (! empty ( $var_week_num [$i] ) || $var_week_num [$i] == 0) && $var_week_num [$i] != '') {
                $save_week [] = $i;
                $save_num1 [] = $var_week_num [$i];
            }
        }
        
        // print_r($save_week);
        // print_r($save_num1);
        

        for($var_ts = $s_ts; $var_ts <= $e_ts; $var_ts += 86400) {
            for($i = 0; $i < count ( $save_week ); $i ++) {
                if (date ( 'w', $var_ts ) == $save_week [$i]) {
                    $save_date [] = $var_ts;
                    $save_num [] = $save_num1 [$i];
                }
            }
        }
        // print_r($save_date);
        // print_r($save_num);
        $gid = bic ();
        for($i = 0; $i < count ( $save_date ); $i ++) {
            $read_info = Dz ( "select * from " . $real_time_table . " where bdid=" . $bdid . " and " . $time_field . "=" . $save_date [$i] . " and del!=1" );
            if (count ( $read_info ) != 0) {
                $read_info = Dz ( "UPDATE " . $real_time_table . " SET " . $num_field . "='" . $save_num [$i] . "' WHERE bdid=" . $bdid . " and " . $time_field . "=" . $save_date [$i] . " AND del!=1" );
            } else {
                $read_info = Dz ( "INSERT INTO " . $real_time_table . " (" . $time_field . "," . $num_field . ",gid,bdb,bdid)
VALUES (" . $save_date [$i] . ",'" . $save_num [$i] . "','" . $gid . "','" . $bdb . "','" . $bdid . "')" );
            }
        }
        
        $timestamp = time ();
        break;
    
    case 'span' :
        $arr = explode ( '-', $_GET ['startWeekDate'] );
        $s_year = $arr [0];
        $s_month = $arr [1];
        $s_day = $arr [2];
        $arr = explode ( '-', $_GET ['endWeekDate'] );
        $e_year = $arr [0];
        $e_month = $arr [1];
        $e_day = $arr [2];
        $s_ts = mktime ( 0, 0, 0, $s_month, $s_day, $s_year );
        $e_ts = mktime ( 0, 0, 0, $e_month, $e_day, $e_year );
        //$save_default_num = $_GET [default_num];
        
        for($var_ts = $s_ts; $var_ts <= $e_ts; $var_ts += 86400) {
            $save_date [] = $var_ts;
            $save_num [] = $save_default_num;
        }
        // print_r($save_date);
        // print_r($save_num);
        $gid = bic ();
        for($i = 0; $i < count ( $save_date ); $i ++) {
            $read_info = Dz ( "select * from " . $real_time_table . " where bdid=" . $bdid . " and " . $time_field . "=" . $save_date [$i] . " and del!=1" );
            if (count ( $read_info ) != 0) {
                $read_info = Dz ( "UPDATE " . $real_time_table . " SET " . $num_field . "='" . $save_num [$i] . "' WHERE bdid=" . $bdid . " and " . $time_field . "=" . $save_date [$i] . " AND del!=1" );
            } else {
                $read_info = Dz ( "INSERT INTO " . $real_time_table . " (" . $time_field . "," . $num_field . ",gid,bdb,bdid)
VALUES (" . $save_date [$i] . ",'" . $save_num [$i] . "','" . $gid . "','" . $bdb . "','" . $bdid . "')" );
            }
        }
        
        $timestamp = time ();
        break;
    
    case 'moren' :
        //$save_default_num = $_GET [default_num];
        $save_year = $_GET ['year'];
        $save_month = $_GET ['month'];
        
        $read_info = Dz ( "select * from " . $real_time_table . " where bdid=" . $bdid . " and " . $time_field . "=1 and del!=1" );
        if (count ( $read_info ) != 0) {
            $read_info = Dz ( "UPDATE " . $real_time_table . " SET price='" . $save_default_num . "' WHERE bdid=" . $bdid . " and " . $time_field . "=1 AND del!=1" );
        } else {
            $read_info = Dz ( "INSERT INTO " . $real_time_table . " (" . $time_field . "," . $num_field . ",gid,bdb,bdid)
VALUES (1,'" . $save_default_num . "','" . $gid . "','" . $bdb . "','" . $bdid . "')" );
        }
        $timestamp = mktime ( 0, 0, 0, $save_month, 1, $save_year );
        break;
    
    case 'save' :
        $var_num = $_GET ['num'];
        $save_year = $_GET ['year'];
        
        $save_month = $_GET ['month'];
        $read_info = Dz ( "select * from " . $real_time_table . " where bdid=" . $bdid . " and date=1 and del!=1" );
        $save_default_num = $read_info [0] ['price'];
        $save_date='';
        for($i = 1; $i < 32; $i ++) {
            if (isset ( $var_num [$i] )  && $var_num [$i] != '') {
                if ($var_num [$i] != $save_default_num) {
                    $save_date [] = mktime ( 0, 0, 0, $save_month, $i, $save_year );
                    $save_num [] = $var_num [$i];
                    $save_room [] = $var_num [$i.'_room'];
                } else {
                    $del_date = mktime ( 0, 0, 0, $save_month, $i, $save_year );
                    $exe = Dz ( "UPDATE " . $real_time_table . " SET del=1 WHERE date=" . $del_date . " and bdid=" . $bdid );
                }
            }
        }
         //Jc($save_date);
         //Jc($save_num);
         //Jc($save_room);
        for($i = 0; $i < count ( $save_date ); $i ++) {
            $read_info = Dz ( "select * from " . $real_time_table . " where bdid=" . $bdid . " and " . $time_field . "=" . $save_date [$i] . " and del!=1" );
            if ( $read_info != '') {
                $read_info = Dz ( "UPDATE " . $real_time_table . " SET " . $num_field . "='" . $save_num [$i] . "',room=".$save_room[$i]." WHERE  bdid=" . $bdid . " and " . $time_field . "=" . $save_date [$i] . " AND del!=1" );
            } else {
                $gid = bic ();
                $read_info = Dz ( "INSERT INTO " . $real_time_table . " (" . $time_field . "," . $num_field . ",room,gid,bdb,bdid)
VALUES (" . $save_date [$i] . ",'" . $save_num [$i] . "',".$save_room[$i].",'" . $gid . "','" . $bdb . "','" . $bdid . "')" );
            }
        }
        
        /**
         * 更新酒店最低价
         */
        $min=Dga('price',"del!=1 and bdid='{$bdid}' and price!=0 order by price");
        $hotel_room=Dga('hotel_room',"gid='{$bdid}'");
        
        $hotel=Dga('hotel',"gid='{$hotel_room['bdid']}'");
        $room=Dz("select gid from lm_hotel_room where 1 and del!=1 {$city} and bdid like '{$hotel['gid']}'");
        $gid=array();
        foreach($room as $room_val){
            $gid[]='"'.$room_val['gid'].'"';
            //$gid=$gid==''?$room_val['gid']:','.$room_val['gid'];
        }
        $gid=implode(',',$gid);
        $date=strtotime(date("Y-m-d"));
        $price=Dga('price',"del!=1 and bdid in ({$gid})");
        
        $lowerPrice['lowerPrice']=$price['price'];
        Du('hotel_room',$lowerPrice,'id',$hotel_room['id']);
        Du('hotel',$lowerPrice,'gid',$hotel_room['bdid']);
        
        
        $timestamp = mktime ( 0, 0, 0, $save_month, 1, $save_year );
        break;
    
    default :
        
        $timestamp = time ();
}

$today_year = date ( 'Y', time () );
$today_month = date ( 'n', time () );
$today_day = date ( 'j', time () );
$today_week = date ( 'w', time () );
$the_year = date ( 'Y', $timestamp );
$the_month = date ( 'n', $timestamp );
$month_days = date ( 't', $timestamp );
$begin_week = date ( "w", mktime ( 0, 0, 0, $the_month, 1, $the_year ) );
$month_s = mktime ( 0, 0, 0, $the_month, 1, $the_year );
$month_e = mktime ( 0, 0, 0, $the_month, $month_days, $the_year );

$read_info = Dz ( "select * from " . $real_time_table . " where bdid=" . $bdid . " and " . $time_field . ">=" . $month_s . " and " . $time_field . "<=" . $month_e . " and del!=1" );
$common_num = Dz ( "select * from " . $real_time_table . " where bdid=" . $bdid . " and " . $time_field . "=1 and del!=1" );

for($i = 0; $i < count ( $read_info ); $i ++) {
    $var_read_day = date ( 'j', $read_info [$i] [$time_field] );
    $read_day [] = $var_read_day;
    $read_num [] = $read_info [$i] [$num_field];
    $room_num [] = $read_info [$i] ['room'];
}
// print_r($read_day);
// print_r($read_num);
//Jc($room_num);


?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-CN" lang="zh-CN">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Prosperous JIMU</title>

<style>
body {
	background-color: #666666;
	font-size: 14px;
}

div {
	background-color: #DFEBF7;
	padding: 50px;
	padding-top: 20px;
	width: 600px;
}

.th {
	font-size: 12px;
	width: 80px;
	height: 45px;
	border: #999999 1px solid;
	background: #5CCAFE;
}

em {
	font-size: 12px;
	width: 70px;
	padding: 0;
	height: 20px;
	line-height: 20px;
	font-style: normal;
}

.input {
	border: #CCCCCC 1px solid;
	line-height: 12px;
	height: 15px;
	padding: 0;
}

.can {
	border: #999999 1px solid;
	width: 450px;
}

.can2 {
	width: 450px;
}

.content {
	width: 500px;
	border: #666666 1px solid;
	padding: 5px;
	padding-left: 50px;
	padding-right: 50px;
}

.STYLE1 {
	color: #FF0000
}

.STYLE2 {
	color: #FF3300
}
</style>
<script language="javascript" type="text/javascript"
	src="../../jimu/My97/WdatePicker.js"></script>


</head>
<body>
<div>实时房态
<div class="content">
<table class="can2" cellpadding="0" cellspacing="0">
	<tr style="font-size: 12px; vertical-align: middle;">
		<th colspan="4">
		<form action="real_time.php" method="GET" name="forward_month">
		<table class="can2" cellpadding="0" cellspacing="0">
			<tr style="font-size: 12px; vertical-align: middle;">
				<th align="center" colspan="3">今日：<?php
    echo $today_year . '年 ' . $today_month . '月 ' . $today_day . '日 ' . '星期' . $today_week?></th>
				<th colspan="4"><input type="hidden" name="op" value="forward" /> <input
					name="year" type="text" size="2" maxlength="6"
					value="<?php
    echo $today_year;
    ?>" /> 年 <select name="month"
					onchange="document.forward_month.submit();">
            <?php
            for($i = 1; $i <= 12; $i ++) {
                if ($today_month == $i) {
                    ?>
            <option
						value="<?php
                    echo $i;
                    ?>"
						selected="selected"><?php
                    echo $i;
                    ?></option>
            <?php
                } else {
                    ?>
            <option
						value="<?php
                    echo $i;
                    ?>"><?php
                    echo $i;
                    ?></option>
            <?php
                }
            }
            ?>
          </select> 月<input type="hidden" name="bdid"
					value="<?php
    echo $_GET [bdid];
    ?>" /> <input type="hidden" name="bdb"
					value="<?php
    echo $_GET [bdb];
    ?>" /> <input type="submit" value="转到" /></th>
			</tr>
		</table>
		</form>

		</th>
	</tr>
</table>
<br />
详细设置：
<div style="border: 1px #666666 solid; width: 450px; padding: 10px;">
<form method="GET" action="real_time.php">
<table class="can2" cellpadding="0" cellspacing="0">
	<tr style="height: 40px; font-size: 16px; vertical-align: text-top;">
		<th colspan="2"><a
			href="real_time.php?op=last&ts=<?php
echo $timestamp;
?>&bdid=<?php
echo $_GET [bdid];
?>&bdb=<?php
echo $_GET [bdb];
?>"
			style="color: #FF3366;">上个月</a></th>
		<th colspan="3"><span><?php
echo $the_year . "年  " . $the_month . "月"?></span></th>
		<th colspan="2"><a
			href="real_time.php?op=next&ts=<?php
echo $timestamp;
?>&bdid=<?php
echo $_GET [bdid];
?>&bdb=<?php
echo $_GET [bdb];
?>"
			style="color: #FF3366;">下个月</a></th>
	</tr>
</table>
<table class="can">
	<tr style="font-size: 12px; height: 30px;">
		<th><span class="STYLE1">星期日</span></th>
		<th>星期一</th>
		<th>星期二</th>
		<th>星期三</th>
		<th>星期四</th>
		<th>星期五</th>
		<th><span class="STYLE2">星期六</span></th>
	</tr>
	<input type="hidden" name="op" value="save" />
	<input type="hidden" name="month" value="<?php
echo $the_month;
?>" />
	<input type="hidden" name="year" value="<?php
echo $the_year;
?>" />
<?php
$var_day = 1;
for($week_num = 0; $week_num < 6; $week_num ++) {
    ?>
     <tr class="tr">
<?php
    for($week_i = 0; $week_i < 7; $week_i ++) {
        
        ?>           <th class="th">
<?php
        if (($week_num == 0 && $week_i < $begin_week) || intval ( $var_day ) > intval ( $month_days )) {
            ?>                <em>&nbsp;</em> <i>&nbsp;</i>
<?php
        } else {
            for($i = 0; $i <= count ( $read_day ); $i ++) {
                if ($read_day [$i] == $var_day) {
                    $input_value = $read_num [$i];
                    $room_value = $room_num [$i];
                    break;
                } else {
                    $input_value = $common_num [0] [$num_field];
                    $room_value = 0;
                }
            }
            
            if ($week_i == 0) {
                ?>
				<em style="color: #FF0000">
				<?php
                echo $the_month . "-" . $var_day;
                ?>
                </em> <i><input class="input" type="text"
			name="num[<?php
                echo $var_day;
                ?>]"
			value="<?php
                echo $input_value;
                ?>"
			size="2" maxlength="6" /></i>
			<!-- ############### 房态 ############### -->
			<i style="display:none;"><input class="input" type="text"
			name="num[<?php
                echo $var_day;
                ?>_room]"
			value="<?php
                echo $room_value;
                ?>"
			size="2" maxlength="6" /></i>
				<?php
            } else {
                ?>              <em><?php
                echo $the_month . "-" . $var_day;
                ?></em> <i><input class="input" type="text"
			name="num[<?php
                echo $var_day;
                ?>]"
			value="<?php
                echo $input_value;
                ?>"
			size="2" maxlength="6" /></i>
			<!-- ############### 房态 ############### -->
			<i style="display:none;"><input class="input" type="text"
			name="num[<?php
                echo $var_day;
                ?>_room]"
			value="<?php
                echo $room_value;
                ?>"
			size="2" maxlength="6" /></i>
<?php
            }
            $var_day ++;
        
        }
        ?>
         </th>
<?php
    }
    ?>
</tr>
<?php
}
?>
<tr style="height: 50px;">
		<th colspan="4"></th>
		<th colspan="2"><input type="hidden" name="bdid"
			value="<?php
echo $_GET ['bdid'];
?>" /> <input type="hidden" name="bdb"
			value="<?php
echo $_GET ['bdb'];
?>" /><input class="edit" type="submit" value="提交" /></th>
		<th colspan="3"><input type="reset" value="重置" /></th>
	</tr>
</table>
</form>
<form action="real_time.php" method="get">
<table class="can2">
	<!--<tr style="height: 40px;">
		<th colspan="5">默认显示价格：<input type="text" name="default_num" size="3"
			maxlength="6" value="<?php
echo $common_num [0] [$num_field];
?>" /></th>
		<th><input type="hidden" name="bdid"
			value="<?php
echo $_GET ['bdid'];
?>" /> <input type="hidden" name="bdb"
			value="<?php
echo $_GET ['bdb'];
?>" /><input type="hidden" name="op" value="moren" /><input
			type="hidden" name="month" value="<?php
echo $the_month;
?>" /><input type="hidden" name="year"
			value="<?php
echo $the_year;
?>" /> <input class="edit" type="submit" value="提交" /></th>
		<th><input type="reset" value="重置" /></th>
	</tr>-->
</table>
</form>
</div>
<br />
按星期设置：
<div style="border: 1px #666666 solid; width: 450px; padding: 10px;">
<form action="real_time.php" method="get"><input type="hidden" name="op"
	value="week" />
<table class="can">
	<tr>
		<th class="th"><span class="STYLE1">星期日</span> <input class="input"
			type="text" name="week_num[0]" size="2" maxlength="6" /></th>
		<th class="th">星期一<input class="input" type="text" name="week_num[1]"
			size="2" maxlength="6" /></th>
		<th class="th">星期二<input class="input" type="text" name="week_num[2]"
			size="2" maxlength="6" /></th>
		<th class="th">星期三<input class="input" type="text" name="week_num[3]"
			size="2" maxlength="6" /></th>
		<th class="th">星期四<input class="input" type="text" name="week_num[4]"
			size="2" maxlength="6" /></th>
		<th class="th">星期五<input class="input" type="text" name="week_num[5]"
			size="2" maxlength="6" /></th>
		<th class="th"><span class="STYLE2">星期六</span> <input class="input"
			type="text" name="week_num[6]" size="2" maxlength="6" /></th>
	</tr>
	<tr>
		<th colspan="2">开始日期:<input id="startWeekDate" name="startWeekDate"
			type="text" size="13" class="Wdate" style="text-align: center;"
			value="<?php
echo date ( "Y-m-d" );
?>"
			onfocus="WdatePicker({isShowClear:false,readOnly:true})" /></th>
		<th colspan="2">結束日期:<input id="endWeekDate" name="endWeekDate"
			type="text" size="13" class="Wdate" style="text-align: center;"
			value="<?php
echo date ( "Y-m-d" );
?>"
			onfocus="WdatePicker({isShowClear:false,readOnly:true})" /></th>
		<th colspan="2"><input type="hidden" name="bdid"
			value="<?php
echo $_GET ['bdid'];
?>" /> <input type="hidden" name="bdb"
			value="<?php
echo $_GET ['bdb'];
?>" /> <input class="edit" type="submit" value="提交" /></th>
		<th><input type="reset" value="重置" /></th>

	</tr>
</table>
</form>
</div>
<br />

按日期区间设置：
<div style="border: 1px #666666 solid; width: 450px; padding: 10px;">
<form action="real_time.php" method="get"><input type="hidden" name="op"
	value="span" />
<table>
	<tr>
		<th colspan="3">开始日期:<input id="startWeekDate" name="startWeekDate"
			type="text" size="13" class="Wdate" style="text-align: center;"
			value="<?php
echo date ( "Y-m-d" );
?>"
			onfocus="WdatePicker({isShowClear:false,readOnly:true})" /></th>
		<th colspan="3">結束日期:<input id="endWeekDate" name="endWeekDate"
			type="text" size="13" class="Wdate" style="text-align: center;"
			value="<?php
echo date ( "Y-m-d" );
?>"
			onfocus="WdatePicker({isShowClear:false,readOnly:true})" /></th>
	</tr>
	<tr>
		<th colspan="4">区间值：<input type="text" name="default_num" size="4"
			maxlength="4" /></th>
		<th colspan="2"><input type="hidden" name="bdid"
			value="<?php
echo $_GET ['bdid'];
?>" /> <input type="hidden" name="bdb"
			value="<?php
echo $_GET ['bdb'];
?>" /> <input class="edit" type="submit" value="提交" /></th>
		<th><input type="reset" value="重置" /></th>

	</tr>
</table>
</form>
</div>
</div>
</div>
</body>
</html>

